package com.whale.cloud.util.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

import lombok.Data;

/** Created by Abel on 2017/6/13. */
@Data
@Component
public class ListOps {

  @Autowired private RedisTemplate<Object, Object> redisTemplate;

  /**
   * 列表设置值
   *
   * @param key
   * @param index
   * @param value
   */
  public boolean setItemValueByIndex(String key, long index, Object value) {

    ListOperations<Object, Object> listOperations = redisTemplate.opsForList();
    listOperations.set(key, index, value);
    return true;
  }

  /**
   * 列表裁剪
   *
   * @param key
   * @param start
   * @param end
   */
  public boolean trim(String key, long start, int end) {

    ListOperations<Object, Object> listOperations = redisTemplate.opsForList();
    listOperations.trim(key, start, end);
    return true;
  }

  /**
   * 移除列表元素 <br>
   * count > 0: 从头到尾依次删除 count 个值为 value 的元素. <br>
   * count < 0: 从尾到头依次删除 count 个值为 value 的元素. <br>
   * count = 0: 删除所有值为 value 的值.
   *
   * @param key
   * @param count
   * @param value
   * @return
   */
  public boolean removeItem(String key, long count, Object value) {
    ListOperations<Object, Object> listOperations = redisTemplate.opsForList();
    listOperations.remove(key, count, value);
    return true;
  }

  /**
   * 获取所有列表项
   *
   * @param key
   * @return
   */
  public List getAllItem(String key) {
    return getItemByRange(key, 0, -1);
  }

  /**
   * 获取指定范围内所有列表项
   *
   * @param key
   * @param start
   * @param end
   * @return
   */
  public List getItemByRange(String key, long start, long end) {
    ListOperations<Object, Object> listOperations = redisTemplate.opsForList();
    return listOperations.range(key, 0, -1);
  }

  /**
   * 获取列表指定位置元素
   *
   * @param key
   * @param index
   * @return
   */
  public Object getItemByIndex(String key, long index) {
    ListOperations<Object, Object> listOperations = redisTemplate.opsForList();
    return listOperations.index(key, index);
  }

  /**
   * 入栈（左进）
   *
   * @param key
   * @param value
   * @return
   */
  public Long push(String key, String value) {
    ListOperations<Object, Object> listOperations = redisTemplate.opsForList();
    return listOperations.leftPush(key, value);
  }

  /**
   * 出栈（左出）
   *
   * @param key
   * @return
   */
  public Object pop(String key) {
    ListOperations<Object, Object> listOperations = redisTemplate.opsForList();
    return listOperations.leftPop(key);
  }

  /**
   * 入队（右进）
   *
   * @param key
   * @param value
   * @return
   */
  public Long in(String key, String value) {
    ListOperations<Object, Object> listOperations = redisTemplate.opsForList();
    return listOperations.rightPush(key, value);
  }

  /**
   * 出队（左出）
   *
   * @param key
   * @return
   */
  public Object out(String key) {
    ListOperations<Object, Object> listOperations = redisTemplate.opsForList();
    return listOperations.leftPop(key);
  }

  /**
   * 获取列表元素个数
   *
   * @param key
   * @return
   */
  public Long getSize(String key) {
    ListOperations<Object, Object> listOperations = redisTemplate.opsForList();
    return listOperations.size(key);
  }
  /**
   * 缓存List数据
   *
   * @param key 缓存的键值
   * @param dataList 待缓存的List数据
   * @return 缓存的对象
   */
  public <T> ListOperations<String, T> setCacheList(String key, List<T> dataList) {
    ListOperations listOperation = redisTemplate.opsForList();
    if (null != dataList) {
      int size = dataList.size();
      for (int i = 0; i < size; i++) {
        listOperation.leftPush(key, dataList.get(i));
      }
    }
    return listOperation;
  }
}
